package com.yyj.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.web.protocol.StoreResponse;

/**
 * 
 * 接口响应信息工具类
 *
 */

public class RspUtils {

    public static <T> BaseResponse<T> success(T data) {
        return success(data, "success");
    }

    public static <T> BaseResponse<T> success() {
        return success(null, "success");
    }

    public static <T> BaseResponse<T> success(T data, String msg) {
        BaseResponse<T> rsp = new BaseResponse<T>();
        rsp.setData(data);
        rsp.setMsg(msg);
        rsp.setRc(ReturnCode.NORMAL_SUCCESS);
        return rsp;
    }

    public static <T> BaseResponse<T> error(String msg) {
        return error(null, msg);
    }

    public static <T> BaseResponse<T> error(T data, String msg) {
        return error(data, msg, ReturnCode.NORMAL_ERROR);
    }

    public static <T> BaseResponse<T> error(T data, String msg, int rc) {
        BaseResponse<T> rsp = new BaseResponse<T>();
        rsp.setData(data);
        rsp.setMsg(msg);
        rsp.setRc(rc);
        return rsp;
    }

    // -----------------------------------------

    public static String errorStr(String msg) {
        JSONObject json = new JSONObject();
        json.put(WebConstant.RETURN_CODE, ReturnCode.NORMAL_ERROR);
        json.put(WebConstant.MESSAGE, msg);
        return json.toJSONString();
    }
    
    public static String errorStr(String msg , int rc) {
    	JSONObject json = new JSONObject();
    	json.put(WebConstant.RETURN_CODE, rc);
    	json.put(WebConstant.MESSAGE, msg);
    	return json.toJSONString();
    }

    public static String successStr(JSONObject data) {
        JSONObject json = new JSONObject();
        json.put("data", data);
        json.put(WebConstant.RETURN_CODE, ReturnCode.NORMAL_SUCCESS);
        json.put(WebConstant.MESSAGE, "success");
        return json.toJSONString();
    }

    public static String needLoginStr() {
        JSONObject json = new JSONObject();
        json.put(WebConstant.RETURN_CODE, ReturnCode.UN_LOGIN);
        json.put(WebConstant.MESSAGE, "请登录");
        return json.toJSONString();
    }
    
    public static String needLoginStr(JSONObject data) {
        JSONObject json = new JSONObject();
        json.put(WebConstant.RETURN_CODE, ReturnCode.UN_LOGIN);
        json.put(WebConstant.MESSAGE, "请登录");
        json.put(WebConstant.DATA, data);
        return json.toJSONString();
    }

    // -----------------------------------------
    public static String sub() {
        JSONObject json = new JSONObject();
        json.put(WebConstant.RETURN_CODE, ReturnCode.THIS_IS_A_SUB);
        json.put(WebConstant.MESSAGE, "This is a sub!");
        return json.toJSONString();
    }

    public static String tokenInvalid() {
        return JSON.toJSONString(error(null, "令牌失效", ReturnCode.TOKEN_INVALID));
    }

    public static String signInvalid() {
        return JSON.toJSONString(error(null, "签名失效", ReturnCode.SIGN_ERROR));
    }
    
    public static void sendAjaxResponse(HttpServletResponse httpResponse , String jsonStr){
		httpResponse.setContentType("text/html;charset=UTF-8"); 
		PrintWriter out;
		try {
			out = httpResponse.getWriter();
	        out.print(jsonStr);  
	        out.flush();  
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

    public static <T> StoreResponse<T> successStore(List<T> data) {
        StoreResponse<T> rsp = new StoreResponse<T>();
        rsp.setRows(data);
        rsp.setMsg("success");
        rsp.setResults(data.size());
        rsp.setHasError(false);
        rsp.setRc(ReturnCode.NORMAL_SUCCESS);
        return rsp;
    }

}
